package com.jzr.bedside.ui.skill

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.utils.TimeUtils
import com.blankj.utilcode.utils.ToastUtils
import com.jzr.bedside.R
import com.jzr.bedside.base.BaseActivity
import com.jzr.bedside.bean.DrugDataBean
import com.jzr.bedside.bean.OrderListBean
import com.jzr.bedside.presenter.contract.DrugActivityContract
import com.jzr.bedside.presenter.impl.DrugActivityImpl
import com.jzr.bedside.ui.adapter.DrugAdapter
import com.jzr.bedside.utils.PreferUtil
import kotlinx.android.synthetic.main.actionbar_root_layout.*
import kotlinx.android.synthetic.main.activity_drug.*
import java.util.*

class DrugActivity : BaseActivity(), View.OnClickListener, DrugActivityContract.View {

    private val mPresenter = DrugActivityImpl()
    private val drugDataBean = mutableListOf<DrugDataBean>()

    private var adapter: DrugAdapter? = null


    override fun getLayoutId(): Int {
        return R.layout.activity_drug
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun detachView() {
        mPresenter.detachView()
    }

    override fun initView() {
        ivRight.setOnClickListener(this)

        mPresenter.getOrderList(this, "admissionId", PreferUtil.getInstance().cureNo)

        adapter = DrugAdapter(drugDataBean)
        rvRecord.adapter = adapter
        val manage = LinearLayoutManager(this)
        manage.orientation = LinearLayoutManager.VERTICAL
        rvRecord.layoutManager = manage

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ivRight -> {
                finish()
            }
        }
    }

    override fun getOrderListSuccess(data: OrderListBean?) {
        val resultMap: MutableMap<String, MutableList<OrderListBean.DataBean>> =
            HashMap()

        for ((index, item) in data?.data?.withIndex()!!) {
            //该日期已存在，则放入该集合
            val time = TimeUtils.string2Millis(item.startTime, "yyyy-MM-dd")
            if (resultMap.containsKey(time.toString())) {
                resultMap[time.toString()]?.add(item)
            } else {
                val itemList: MutableList<OrderListBean.DataBean> = ArrayList()
                itemList.add(item)
                resultMap[time.toString()] = itemList
            }
        }

        val drugDataBeanList = mutableListOf<DrugDataBean>()
        for ((key, value) in resultMap) {
            val drugDataBean = DrugDataBean()
            drugDataBean.time = key
            drugDataBean.dataBeanList = value
            drugDataBeanList.add(drugDataBean)
        }

        Collections.sort(drugDataBeanList, Comparator<DrugDataBean?> { o1, o2 ->
            if (o1?.time?.toLong()!! < o2?.time?.toLong()!!) {
                return@Comparator 1
            }
            if (o1.time?.toLong()!! == o2.time?.toLong()!!) {
                0
            } else -1
        })

        adapter?.let {
            it.setNewData(drugDataBeanList)
            it.notifyDataSetChanged()
        }

    }

    override fun showError(message: String?) {
        ToastUtils.showShortToast(message)
    }


}
